package com.yinxin.thread;

import com.yinxin.util.WebDownLoader;

import java.util.concurrent.*;

/**
 * @author yinxin
 * @Title:
 * @Package
 * @Description: 实现Callable接口
 * @date 2022/1/710:47
 */
public class TestCallable implements Callable<Boolean> {

    private String url;
    private String pageName;
    public TestCallable(String url,String pageName){
        this.pageName=pageName;
        this.url=url;
    }
    @Override
    public Boolean call() throws Exception {
        WebDownLoader webDownLoader = new WebDownLoader();
        webDownLoader.downLoader(url,pageName);
        System.out.println("下载了文件名为："+pageName);
        return true;
    }


    public static void main(String[] args) throws ExecutionException, InterruptedException {
        TestCallable t1 = new TestCallable("https://cdn.cnbj1.fds.api.mi-img.com/product-images/mi120mmgce/3402.jpg", "t1.jpg");
        TestCallable t2 = new TestCallable("https://cdn.cnbj1.fds.api.mi-img.com/product-images/redmik40/seciton2-1.jpg", "t2.jpg");
        TestCallable t3 = new TestCallable("https://cdn.cnbj1.fds.api.mi-img.com/product-images/redmik40/seciton3-1.jpg", "t3.jpg");
        //创建执行服务
        ExecutorService executorService = Executors.newFixedThreadPool(3);

        //提交执行
        Future<Boolean> result1 = executorService.submit(t1);
        Future<Boolean> result2 = executorService.submit(t2);
        Future<Boolean> result3 = executorService.submit(t3);

        //获取结果
        Boolean r1 = result1.get();
        Boolean r2 = result2.get();
        Boolean r3 = result3.get();
        System.out.println("t1:"+ r1 + "t2:"+ r2 + "t3" + r3 + "下载成功!");

        //关闭服务
        executorService.shutdown();

    }
}
